package com.ldu.houserent.common.config;

import com.ldu.houserent.common.constant.Constant;
import com.ldu.houserent.common.interceptor.AdminInterceptor;
import com.ldu.houserent.common.interceptor.CustomerInterceptor;
import com.ldu.houserent.common.interceptor.OwnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * 前端配置类
 */
@Configuration  //SpringBoot启动时自动加载
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {    //实现接口
    /*
        配置静态资源访问路径
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        //配置上传路径
        registry.addResourceHandler("/uploads/**").addResourceLocations("file:///"+Constant.UPLOADS_PATH);
    }

    /**
     * 配置jsp的访问前后缀
     */
    @Bean
    public InternalResourceViewResolver setupViewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/jsp/");    //设置前缀
        resolver.setSuffix(".jsp");     //设置后缀      合并后的路径是/jsp/front/index.jsp
        return resolver;
    }

    /*
        调用AdminInterceptor，实现拦截器的功能
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {    //从上往下，权限逐渐增大
        //租客
        registry.addInterceptor(new CustomerInterceptor())
                .addPathPatterns("/admin/profile")
                .addPathPatterns("/admin/home")
                .addPathPatterns("/admin/order")
                .addPathPatterns("/admin/mark")
                .addPathPatterns("/admin/feedback")
                .addPathPatterns("/admin/password");
        //房东
        registry.addInterceptor(new OwnerInterceptor())
                .addPathPatterns("/admin/house");
        //管理员
        registry.addInterceptor(new AdminInterceptor())
                .addPathPatterns("/admin/news")
                .addPathPatterns("/admin/user")
                .addPathPatterns("/admin/charts");
    }


}
